package com.jz.community.moduleshoppingguide.home.ui.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jz.community.basecomm.base.BaseMvpFragment;
import com.jz.community.basecomm.constant.AppConstants;
import com.jz.community.commview.view.MyStateView;
import com.jz.community.moduleshoppingguide.R;
import com.jz.community.moduleshoppingguide.R2;
import com.jz.community.moduleshoppingguide.home.bean.HotSaleAllBean;
import com.jz.community.moduleshoppingguide.home.bean.HotSaleShowBean;
import com.jz.community.moduleshoppingguide.home.presenter.BestSellersPresenter;
import com.jz.community.moduleshoppingguide.home.ui.BestSellersView;
import com.jz.community.moduleshoppingguide.home.ui.adapter.BestSellersAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;

import butterknife.BindView;

public class BestSellersFragment extends BaseMvpFragment<BestSellersView.View, BestSellersPresenter> implements BestSellersView.View, OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {

    @BindView(R2.id.rv_shopping_guide)
    RecyclerView rvShoppingGuide;
    @BindView(R2.id.shopping_guide_refresh)
    SmartRefreshLayout shoppingGuideRefresh;

    private String goodsCategoryId;
    private int message;
    private int page = 0;
    private int size = 10;
    private BestSellersAdapter adapter;

    MyStateView myStateView;
    private View noDataView;
    private View errorView;
    private View loadingView;

    public static BestSellersFragment newInstance(String goodsCategoryId, int message) {
        Bundle bundle = new Bundle();
        bundle.putString("goodsCategoryId", goodsCategoryId);
        bundle.putInt("message", message);
        BestSellersFragment fragment = new BestSellersFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.module_shoppingguide_fragment_shopping_guide_page;
    }

    @Override
    protected BestSellersPresenter createPresenter() {
        return new BestSellersPresenter(this);
    }


    @Override
    protected void initDatas() {
        shoppingGuideRefresh.setOnRefreshListener(this);//刷新监听
        shoppingGuideRefresh.setEnableLoadMore(false);
        rvShoppingGuide.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        adapter = new BestSellersAdapter(R.layout.module_shoppingguide_item_shopping_guide, new ArrayList<>());
        rvShoppingGuide.setAdapter(adapter);
        adapter.setOnLoadMoreListener(this, rvShoppingGuide);

        myStateView = new MyStateView();
        noDataView = myStateView.getNoDataView(getActivity(), (ViewGroup) rvShoppingGuide.getParent(), "暂无商品！", null);
        loadingView = myStateView.getLoadingView(getActivity(), (ViewGroup) rvShoppingGuide.getParent(), R.drawable.loading_default);

        errorView = myStateView.getErrorView(getActivity(), (ViewGroup) rvShoppingGuide.getParent(), v -> {
            page = 0;
            adapter.setEmptyView(loadingView);
            mPresenter.chunnelHotSaleAll(page, size, goodsCategoryId, AppConstants.APPID, message, AppConstants.ONREFRESH);
        });
    }

    @Override
    protected void initLazy(@Nullable Bundle savedInstanceState) {
        super.initLazy(savedInstanceState);
        goodsCategoryId = getArguments().getString("goodsCategoryId");
        message = getArguments().getInt("message");
        adapter.setMessage(message);
        if (!TextUtils.isEmpty(goodsCategoryId)) {
            page = 0;
            mPresenter.chunnelHotSaleAll(page, size, goodsCategoryId, AppConstants.APPID, message, AppConstants.ONREFRESH);
        } else {
            adapter.setEmptyView(noDataView);
        }
    }


    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        if (!TextUtils.isEmpty(goodsCategoryId)) {
            page = 0;
            mPresenter.chunnelHotSaleAll(page, size, goodsCategoryId, AppConstants.APPID, message, AppConstants.ONREFRESH);
        } else {
            adapter.setEmptyView(noDataView);
            adapter.loadMoreEnd();
            shoppingGuideRefresh.finishRefresh();
        }
    }

    @Override
    public void onLoadMoreRequested() {
        if (!TextUtils.isEmpty(goodsCategoryId)) {
            page++;
            mPresenter.chunnelHotSaleAll(page, size, goodsCategoryId, AppConstants.APPID, message, AppConstants.ONLOADMORE);
        } else {
            adapter.setEmptyView(noDataView);
            adapter.loadMoreEnd();
            shoppingGuideRefresh.finishRefresh();
        }
    }

    @Override
    public void onFail(String s) {
        adapter.setEmptyView(errorView);
        adapter.loadMoreEnd();
        shoppingGuideRefresh.finishRefresh();
    }

    @Override
    public void getHotSaleAll(HotSaleAllBean hotSaleAllBean, int type) {
        shoppingGuideRefresh.finishRefresh();
        if (hotSaleAllBean != null && hotSaleAllBean.get_embedded() != null && hotSaleAllBean.get_embedded().getContent() != null && hotSaleAllBean.get_embedded().getContent().size() > 0) {
            if (type == AppConstants.ONREFRESH) {
                adapter.setNewData(hotSaleAllBean.get_embedded().getContent());
            } else {
                adapter.addData(hotSaleAllBean.get_embedded().getContent());
                adapter.loadMoreComplete();
            }
        } else {
            adapter.loadMoreEnd();
        }
        if (adapter.getItemCount() == 0) {
            adapter.setEmptyView(noDataView);
        }
    }

    @Override
    public void getHotSaleShow(HotSaleShowBean hotSaleShowBean) {

    }
}
